package com.webank.aigc.service.model;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class example {
    public static void main(String[] args) {
        StringBuilder output = null;
        try {
            // 指定Python脚本的路径
            String pythonScriptPath = "D:\\project\\Weidea\\aigc\\src\\main\\java\\com\\webank\\aigc\\service\\script.py";

            // 创建ProcessBuilder对象，传入Python解释器和脚本路径
            ProcessBuilder processBuilder = new ProcessBuilder("D:\\ide\\python\\python.exe", pythonScriptPath, "苹果是什么");

            // 启动进程
            Process process = processBuilder.start();

            // 获取进程的输出流
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

            // 读取输出流中的字符串
            String line;
            output = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
                output.append(line).append(" ");
            }

            // 等待进程结束
            int exitCode = process.waitFor();
            System.out.println("Exit code: " + exitCode);

            // 打印Python脚本的输出
            System.out.println("Output from Python script: ");
            System.out.println(output);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.print(output);
    }
}
